<script>
import { reactive, toRefs, onMounted, ref } from "vue";
import { getReply } from "../assets/api/reply.js";
import { useRouter } from "vue-router";

export default {
  setup() {
    // Date.now()
    let router = useRouter();

    let content = ref(null);
    const list = reactive({
      value: "",
      send: [],
      receive: [],
      locaList: [],
      locaLists: [],
      getList: [],
    });
    function time() {
      let d = new Date();
      let hour = d.getHours();
      hour = hour > 9 ? hour : "0" + hour.toString();
      var minute = d.getMinutes();
      minute = minute > 9 ? minute : "0" + minute.toString();
      var second = d.getSeconds();
      second = second > 9 ? second : "0" + second.toString();
      return `${hour}:${minute}:${second}`;
    }
    // 添加问候
    let add_info = () => {
      list.locaLists = [];
      list.send.push({
        id: Date.now(),
        name: list.value,
      });
      getReply({
        key: "free",
        appid: "0",
        msg: list.value,
      }).then((data) => {
        console.log(data);
        list.receive.push({
          id: Date.now(),
          name: data.content,
        });
      });
      console.log(list.send);
      list.value = "";
    };
    const goBack = () => {
      router.go(-1);
      console.log(list.locaLists);

      list.locaLists.push({
        matchId: list.locaList.matchId,
        content: content.value.innerHTML,
        anchorAvatar: list.locaList.anchorAvatar,
        anchorName: list.locaList.anchorName,
      });
      list.getList = list.getList.filter((item)=>{
        return item.matchId != list.locaList.matchId
      })
      list.getList.push(list.locaLists[0]);
      console.log(list.locaLists);
      window.localStorage.setItem("userInfo", JSON.stringify(list.getList));
    };
    onMounted(() => {
      if (window.localStorage.getItem("userInfo")) {
        list.getList = JSON.parse(window.localStorage.getItem("userInfo"));
        
      } else {
      window.localStorage.setItem("userInfo", JSON.stringify([]));
      }
      list.locaList = JSON.parse(window.localStorage.getItem("userList"));
    });
    return {
      time,
      ...toRefs(list),
      add_info,
      goBack,
      content,
    };
  },
};
</script>

<template>
  <div>
    <div class="container chat">
      <div class="top">
        <i @click="goBack" class="iconfont icon-zuobian"></i
        ><span>{{ locaList.anchorName }}</span>
      </div>
      <div class="time">
        {{ time() }}
      </div>
      <meta name="referrer" content="no-referrer" />
      <div class="chat_content"  v-html="locaList.content"></div>

      <div class="chat_content" ref="content">
        <div class="chat_content_left" v-show="receive.length >= 1">
          <div
            class="chat_content_left_item"
            v-for="item in receive"
            :key="item.id"
          >
            <img :src="locaList.anchorAvatar" alt="" />
            <p class="text">{{ item.name }}</p>
          </div>
        </div>
        <div class="chat_content_right" v-show="send.length >= 1">
          <div
            class="chat_content_right_item"
            v-for="item in send"
            :key="item.id"
          >
            <p class="text">{{ item.name }}</p>
            <img
              src="https://panqiu8-1309958500.cos.ap-guangzhou.myqcloud.com/images/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221029101636.jpg"
              alt=""
            />
          </div>
        </div>
      </div>
      <div class="reply-inp">
        <div class="inp">
          <input
            type="text"
            placeholder="请发表你的评论"
            v-model="value"
            @keydown.enter="add_info"
          />
          <div class="btn" @click="add_info">发送</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.time {
  text-align: center;
  padding: 10px 0;
}
.chat_content {
  padding: 0 15px;
  overflow-y: scroll;
  display: flex;
  justify-content: flex-end;
  .chat_content_right {
    width: 50%;
    .chat_content_right_item {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      margin-bottom: 60px;
      .text {
        position: relative;
        max-width: 120px;
        font-size: 12px;
        background: linear-gradient(270deg, #7b0b3b 0%, #d51c78 100%);
        border-radius: 10px;
        color: #fff;
        padding: 5px 14px;
        margin-right: 14px;
        word-wrap: break-word;
        &::before {
          display: inline-block;
          vertical-align: middle;
          position: absolute;
          right: -8px;
          top: 6px;
          border-top: 8px solid #7b0b3b;
          border-left: 5px solid transparent;
          border-right: 5px solid transparent;
          transform: rotate(268deg);
          content: "";
        }
      }
      img {
        width: 38px;
        height: 38px;
        border-radius: 50%;
        overflow: hidden;
      }
    }
  }
  .chat_content_left {
    width: 50%;
    .chat_content_left_item {
      display: flex;
      align-items: center;
      margin-top: 60px;
      .text {
        position: relative;
        display: flex;
        justify-content: center;
        max-width: 120px;
        flex-wrap: wrap;
        font-size: 12px;
        background: linear-gradient(270deg, #d51c78 0%, #7b0b3b 100%);
        border-radius: 10px;
        color: #fff;
        padding: 5px 14px;
        margin-left: 14px;
        &::before {
          content: "";
          display: inline-block;
          vertical-align: middle;
          position: absolute;
          left: -9px;
          top: 7px;
          border-top: 8px solid #7b0b3b;
          border-left: 5px solid transparent;
          border-right: 5px solid transparent;
          transform: rotate(91deg);
        }
      }
      img {
        width: 38px;
        height: 38px;
        border-radius: 50%;
        overflow: hidden;
      }
    }
  }
}
.chat {
  .top {
    padding: 15px;
    display: flex;
    align-items: center;
    height: 45px;
    color: #fff;
    background: linear-gradient(270deg, #7b0b3b 0%, #d51c78 100%);
    box-shadow: 1px 2px 2px 1px #f5f5f5;
    .iconfont {
      margin-right: 10px;
    }
  }
  .content {
    margin-top: 25px;
    .time {
      text-align: center;
      color: #8c8d8f;
      font-size: 12px;
      margin-bottom: 10px;
    }
    ul {
      li {
        margin-bottom: 10px;
        padding: 5px 15px;
        display: flex;
        justify-content: flex-end;
        img {
          width: 38px;
          height: 38px;
          border-radius: 50%;
        }
        .text {
          // display: inline-block;
          margin-left: 16px;
          margin-right: 14px;
          padding: 10px;
          max-width: 250px;
          font-size: 14px;
          word-wrap: break-word;
          border-radius: 5px;
          height: 100%;
          line-height: 130%;
          position: relative;
          color: #fff;
          background: linear-gradient(270deg, #7b0b3b 0%, #d51c78 100%);
          &::before {
            display: inline-block;
            vertical-align: middle;
            position: absolute;
            right: -8px;
            top: 7px;
            border-top: 8px solid #8e4ff5;
            border-left: 5px solid transparent;
            border-right: 5px solid transparent;
            transform: rotate(-90deg);
            content: "";
          }
        }
      }
    }
  }
}

.reply-inp {
  padding: 10px 20px;
  position: fixed;
  left: 0;
  bottom: 0;
  background-color: #fff;
  .inp {
    display: flex;
    // margin: 11px 0 0;
    align-items: flex-end;
    justify-content: space-between;
    input {
      width: 262px;
      height: 30px;
      line-height: 30px;
      border-radius: 6px;
      font-size: 12px;
      padding: 0 10px;
      color: #757575;
      background: #f2f2f4;
      outline: none;
      border: none;
      margin-right: 18px;
    }
    .btn {
      width: 56px;
      font-size: 12px;
      border-radius: 6px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      color: #fff;
      background: linear-gradient(270deg, #7b0b3b 0%, #d51c78 100%);
    }
  }
}
</style>
